package com.colin.jipay.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Build;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;

import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Util {
    /**
     * 反射得到组件的id号
     */
    public static int getCompentID(String packageName, String className,String idName) {
        int id = 0;
        try {
            Class<?> cls = Class.forName(packageName + ".R$" + className);
            id = cls.getField(idName).getInt(cls);
        } catch (Exception e) {
            LogUtils.i( "缺少" + idName + "文件!");
            e.printStackTrace();
        }
        return id;
    }

    public static int getResouceId(String idName,String packageName){
        int id= Utils.getApp().getResources().getIdentifier(idName,"id",packageName);//通过名字获取id 其值为R.id.yhz
        return id;
    }

    public static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

    /**
     * 判断activity是否可用
     * @param activity
     * @return true:可用
     */
    public static boolean isActivityUseable(Activity activity){
        if (activity==null||activity.isFinishing()||activity.isRestricted()){
            return false;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (activity.isDestroyed())
                return false;
        }
        return true;
    }


    public static Map<String, String> jsonToMap(String var0) {
        HashMap var1 = new HashMap();

        try {
            JSONObject var2 = JSONUtil.getJSONObject(var0);
            Iterator var3 = var2.keys();

            while(var3.hasNext()) {
                String var4 = (String)var3.next();
                var1.put(var4, var2.get(var4) + "");
            }
        } catch (Exception var5) {
            LogUtils.i(var5);
        }

        return var1;
    }

    //截图的方法

    public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength){
        if(null == bitmap || edgeLength <= 0){
            return  null;
        }
        Bitmap result = bitmap;
        int widthOrg = bitmap.getWidth();
        int heightOrg = bitmap.getHeight();
    /*   if(widthOrg > edgeLength && heightOrg > edgeLength)
      {*/
        //压缩到一个最小长度是edgeLength的bitmap
        int longerEdge = (int)(edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg));
        int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
        int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
        Bitmap scaledBitmap;
        try{
            scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
        }
        catch(Exception e){
            return null;
        }
        //从图中截取正中间的正方形部分。
        int xTopLeft = (scaledWidth - edgeLength) / 2;
        int yTopLeft = (scaledHeight - edgeLength-600) / 2;
        try{
            result = Bitmap.createBitmap(scaledBitmap, 200, 400, 600, 600);
            scaledBitmap.recycle();
        }
        catch(Exception e){
            return null;
        }
        /* }  */
        return result;
    }


    public static void saveMyBitmap(Bitmap mBitmap,String bitName)  {
        File f = new File( "/sdcard/ewmtp/"+bitName + ".jpg");
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //删除截图

    public static void deleteimg(String path){
        File file=new File(path);
        file.delete();
    }
}
